/**
 * 
 */
package ua.lviv.franko.mlsFunctions.twoDimension.weightFunctions;

import org.jzy3d.maths.Range;
import org.jzy3d.plot3d.builder.Mapper;

import ua.lviv.franko.mlsFunctions.oneDimension.weightFunctions.CubicSpline;
import ua.lviv.franko.mlsFunctions.oneDimension.weightFunctions.IWeightFunction;
import ua.lviv.franko.mlsFunctions.twoDimension.Particle;
import ua.lviv.franko.visualisation.Visualise3D;

/**
 * @author marko
 * 
 */
public class CircleSupport implements IWeightFunction2D {

	protected IWeightFunction	func;

	public CircleSupport(IWeightFunction func) {
		this.func = func;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * mlsFunctions.twoDimension.weightFunctions.IWeightFunction2D#calculate
	 * (double, double, double, double, double)
	 */
	@Override
	public double calculate(double x, double y, double xi, double yi, double rho) {
		double val = ((x - xi) * (x - xi) + (y - yi) * (y - yi)) / rho;
		return func.calculate(val, 0, 1);
		// return func.calculate(x*x+y*y, xi*xi+yi*yi, rho);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mlsFunctions.twoDimension.weightFunctions.IWeightFunction2D#
	 * calculateDerivatyX(double, double, double, double, double)
	 */
	@Override
	public double calculateDerivatyX(double x, double y, double xi, double yi, double rho) {
		double val = ((x - xi) * (x - xi) + (y - yi) * (y - yi)) / rho;
		return func.calculateDerivaty(val, 0, 1) * (x - xi) / rho;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mlsFunctions.twoDimension.weightFunctions.IWeightFunction2D#
	 * calculateDerivatyY(double, double, double, double, double)
	 */
	@Override
	public double calculateDerivatyY(double x, double y, double xi, double yi, double rho) {
		double val = ((x - xi) * (x - xi) + (y - yi) * (y - yi)) / rho;
		return func.calculateDerivaty(val, 0, 1) * (y - yi) / rho;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mlsFunctions.twoDimension.weightFunctions.IWeightFunction2D#
	 * intersectedSupport(mlsFunctions.twoDimension.Particle,
	 * mlsFunctions.twoDimension.Particle)
	 */
	@Override
	public boolean intersectedSupport(Particle p1, Particle p2) {
		return ((p1.lengthTo(p2) - (p1.getRho() + p2.getRho()) / Math.sqrt(2.0)) < 0.00000001);
	}

	public static void main(String[] args) {
		final CircleSupport ffff = new CircleSupport(new CubicSpline());
		Visualise3D.show(new Mapper() {

			@Override
			public double f(double x, double y) {
				return ffff.calculateDerivatyY(x, y, 0.5, 0.5, 0.25);// (x, y);
			}
		}, new Range(0.0, 1.0), new Range(0.0, 1.0));
	}

}
